package run.bottle.mail.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import run.bottle.mail.model.entity.BaseResponse;
import run.bottle.mail.model.entity.Email;
import run.bottle.mail.model.enums.EmailType;

/**
 * 描述
 *
 * @author LiYuanCheng
 * @date 2020-06-05 14:31
 */
@Component
public class TextEmailHandler implements EmailHandler {

  private static final Logger logger = LoggerFactory.getLogger(TextEmailHandler.class);

  @Autowired
  private JavaMailSender mailSender;

  /**
   * 发送人
   */
  @Value("${spring.mail.username}")
  public String userName;

  @Override
  public BaseResponse send(Email mail) {
    logger.info("发送邮件：{}",mail.getContent());
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(userName);
    message.setTo(mail.getEmail());
    message.setSubject(mail.getSubject());
    message.setText(mail.getContent());
    mailSender.send(message);
    return BaseResponse.ok("发送成功！");
  }

  @Override
  public EmailType getEmailType() {
    return EmailType.TEXT;
  }
}
